import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.text.*;
/**
 * La clase PathPlanning agrupa los datos que afectan 
 * la planificación de la ruta de corte.
 */
public class PathPlanning extends DataCategory
{
    public PathPlanning()
    {
    }
    public boolean initData(int id,int value)
    {
        switch(id)
        {
            case MOTION_TYPE:
                motionType = Integer.valueOf(value);
                return true;
            case CARTESIAN_PLANE:
                cartesianPlane = Integer.valueOf(value);
                return true;
            case DEFAULT_METRIC_SYSTEM:
                defaultMetricSystem = Integer.valueOf(value);
                return true;
            case METRIC_SYSTEM:
                metricSystem = Integer.valueOf(value);
                return true;
            case DIMENSIONING_MODE:
                dimMode = Integer.valueOf(value);
                return true;
            case DEFAULT_DIMENSIONING_MODE:
                defaultDimMode = Integer.valueOf(value);
                return true;
            case ATP_X:
                xATP = Integer.valueOf(value);
                return true;
            case ATP_Y:
                yATP = Integer.valueOf(value);
                return true;
            case ATP_Z:
                zATP = Integer.valueOf(value);
                return true;
            case ACTIVE_WORK_OFFSET_INDEX:
                activeWOIndex = Integer.valueOf(value);
                return true;
            case ACTIVE_TOOL_OFFSET_INDEX:
                activeTOIndex = Integer.valueOf(value);
                return true;
            case NUM_WORK_OFFSETS:
                numberWorkOffset = Integer.valueOf(value);
                return true;
            default:
                return false;
        }
    }
    public boolean initData(int id,long value)
    {
        return initData(id,(int) value);
    }
    public Component getView(Id viewID)
    {
        JPanel panel = new JPanel();
        JPanel p_aux;
        JPanel p_aux1;
        JPanel p_aux2;
        JPanel p_aux3;
        GridBagConstraints constraints;
        switch(viewID)
        {
            case MOTION_TYPE:
                panel.setLayout(new GridLayout(1,2));
                p_aux = new JPanel();
                JLabel mtnTypeLabel = 
                    NewComp.getLabel("motionType","titleLabelTFrame","blue");
                motionTypeField = NewComp.getTxtField(motionTypeOpt[motionType], "textFieldTFrame");
                p_aux.add(motionTypeField);
                panel.add(mtnTypeLabel);
                panel.add(p_aux);
                return panel;
            case CARTESIAN_PLANE:
                panel.setLayout(new GridLayout(1,2));
                p_aux = new JPanel();
                JLabel cartesianPlaneLabel = 
                    NewComp.getLabel(
                        "cartesianPlane",
                        "titleLabelTFrame",
                        "blue");
                cartesianPlaneField = 
                    NewComp.getTxtField(
                        cartesianPlaneOpt[cartesianPlane], 
                        "textFieldTFrame");
                p_aux.add(cartesianPlaneField);
                panel.add(cartesianPlaneLabel);
                panel.add(p_aux);
                return panel;
            case METRIC_SYSTEM:
                panel.setLayout(new GridLayout(1,2));
                p_aux = new JPanel();
                JLabel metricSystemLabel = NewComp.getLabel("metricSystem","titleLabelTFrame","blue");
                metricSystemField = NewComp.getTxtField(metricSystemOpt[metricSystem], "textFieldTFrame");
                p_aux.add(metricSystemField);
                panel.add(metricSystemLabel);
                panel.add(p_aux);
                return panel;
            case DIMENSIONING_MODE:
                panel.setLayout(new GridLayout(1,2));
                p_aux = new JPanel();
                JLabel dimModeLabel = NewComp.getLabel("dimMode","titleLabelTFrame","blue");
                dimModeField = NewComp.getTxtField(dimModeOpt[dimMode], "textFieldTFrame");
                p_aux.add(dimModeField);
                panel.add(dimModeLabel);
                panel.add(p_aux);
                return panel;
            case ATP:
                panel.setLayout(new GridLayout(3,2,8,0));
                p_aux1 = new JPanel(new GridBagLayout());
                p_aux2 = new JPanel(new GridBagLayout());
                p_aux3 = new JPanel(new GridBagLayout());
                JLabel xLabel = NewComp.getLabel("x","titleLabelTFrame","blue");
                JLabel yLabel = NewComp.getLabel("y","titleLabelTFrame","blue");
                JLabel zLabel = NewComp.getLabel("z","titleLabelTFrame","blue");
                xATPField = NewComp.getFTF(metricSystemOpt[metricSystem].equals("international"),"textFieldNumTFrame");
                yATPField = NewComp.getFTF(metricSystemOpt[metricSystem].equals("international"),"textFieldNumTFrame");
                zATPField = NewComp.getFTF(metricSystemOpt[metricSystem].equals("international"),"textFieldNumTFrame");
                xATPField.setValue(xATP);
                yATPField.setValue(yATP);
                zATPField.setValue(zATP);
                constraints = new GridBagConstraints();
                constraints.anchor = GridBagConstraints.CENTER;
                p_aux1.add(xATPField,constraints);
                p_aux2.add(yATPField,constraints);
                p_aux3.add(zATPField,constraints);
                panel.add(xLabel);
                panel.add(p_aux1);
                panel.add(yLabel);
                panel.add(p_aux2);
                panel.add(zLabel);
                panel.add(p_aux3);
                return panel;
            case PROGRAM_ZERO:
                panel.setLayout(new GridLayout(3,2));
                p_aux1 = new JPanel(new GridBagLayout());
                p_aux2 = new JPanel(new GridBagLayout());
                p_aux3 = new JPanel(new GridBagLayout());
                JLabel xLabel_ = NewComp.getLabel("x","titleLabelTFrame","blue");
                JLabel yLabel_ = NewComp.getLabel("y","titleLabelTFrame","blue");
                JLabel zLabel_ = NewComp.getLabel("z","titleLabelTFrame","blue");
                xProgramZeroField = NewComp.getFTF(metricSystemOpt[metricSystem].equals("international"),"textFieldNumTFrame");
                xProgramZeroField.setValue(new Integer(0));
                yProgramZeroField = NewComp.getFTF(metricSystemOpt[metricSystem].equals("international"),"textFieldNumTFrame");
                yProgramZeroField.setValue(new Integer(0));
                zProgramZeroField = NewComp.getFTF(metricSystemOpt[metricSystem].equals("international"),"textFieldNumTFrame");
                zProgramZeroField.setValue(new Integer(0));
                if(!offsetAxes.isEmpty()){
                    xProgramZeroField.setValue(new Integer(offsetAxes.get(activeWOIndex)[0]));
                    yProgramZeroField.setValue(new Integer(offsetAxes.get(activeWOIndex)[1]));
                    zProgramZeroField.setValue(new Integer(offsetAxes.get(activeWOIndex)[2]));
                }
                constraints = new GridBagConstraints();
                constraints.anchor = GridBagConstraints.CENTER;
                p_aux1.add(xProgramZeroField,constraints);
                p_aux2.add(yProgramZeroField,constraints);
                p_aux3.add(zProgramZeroField,constraints);
                panel.add(xLabel_);
                panel.add(p_aux1);
                panel.add(yLabel_);
                panel.add(p_aux2);
                panel.add(zLabel_);
                panel.add(p_aux3);
                return panel;
            default:
                return null;
        }
    }
    public Component getEditor(Id editorID)
    {
        JPanel panel = new JPanel();
        JPanel panel_Aux;
        JPanel panel_Aux1;
        JPanel panel_Aux2;
        JPanel panel_Aux3;
        ButtonGroup group;
        int i;
        switch(editorID)
        {
            case DEFAULT_METRIC_SYSTEM:
                defaultMetricSystem = metricSystem;
                group = new ButtonGroup();
                panel = NewComp.getPanel("defaultMetricSystem","titleBorderBFrame",new GridLayout(1,2));
                international =NewComp.getRadioButton( 
                    "defInternational", 
                    "titleRButton", 
                    "blue",
                    metricSystemOpt[defaultMetricSystem].equals("international"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            defaultMetricSystem = Integer.valueOf(0);
                            if(changed.lastIndexOf(Id.DEFAULT_METRIC_SYSTEM) == -1)
                                changed.add(Id.DEFAULT_METRIC_SYSTEM);
                        }
                    });
                english =NewComp.getRadioButton( 
                    "defEnglish", 
                    "titleRButton",
                    "blue",
                    metricSystemOpt[defaultMetricSystem].equals("english"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            //InfoManager.addDataValue(getId(Data.METRIC_SYSTEM), 1);
                            defaultMetricSystem = Integer.valueOf(1);
                            if(changed.lastIndexOf(Id.DEFAULT_METRIC_SYSTEM) == -1)
                                changed.add(Id.DEFAULT_METRIC_SYSTEM);
                        }
                    });
                group.add(international);
                group.add(english);
                panel.add(international);
                panel.add(english);
                return panel;
            case DEFAULT_DIMENSIONING_MODE:
                group = new ButtonGroup();
                defaultDimMode = dimMode;
                panel = NewComp.getPanel("defaultDimMode","titleBorderBFrame",new GridLayout(1,2));
                absolute =NewComp.getRadioButton( 
                    "absolute", 
                    "titleRButton",
                    "blue",
                    dimModeOpt[defaultDimMode].equals("absolute"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            defaultDimMode = Integer.valueOf(0);
                            if(changed.lastIndexOf(Id.DEFAULT_DIMENSIONING_MODE) == -1)
                                changed.add(Id.DEFAULT_DIMENSIONING_MODE);
                        }
                    });
                incremental =NewComp.getRadioButton( 
                    "incremental", 
                    "titleRButton",
                    "blue",
                    dimModeOpt[defaultDimMode].equals("incremental"), 
                    new ActionListener()
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            defaultDimMode = Integer.valueOf(1);
                            if(changed.lastIndexOf(Id.DEFAULT_DIMENSIONING_MODE) == -1)
                                changed.add(Id.DEFAULT_DIMENSIONING_MODE);

                        }
                    });
                group.add(absolute);
                group.add(incremental);
                panel.add(absolute);
                panel.add(incremental);
                return panel;
            case WORK_OFFSET:
                changeNmToCurrentSys();
                listIndexWorkOffsetX = new boolean[numberWorkOffset];
                listIndexWorkOffsetY = new boolean[numberWorkOffset];
                listIndexWorkOffsetZ = new boolean[numberWorkOffset];
                panel = NewComp.getPanel(
                    "workOffset",
                    "titleBorderBFrame",
                    new GridLayout(4,1)
                );
                panel_Aux = new JPanel();
                panel_Aux1 = new JPanel();
                panel_Aux2 = new JPanel();
                panel_Aux3 = new JPanel();
                JLabel workOXLabel =  NewComp.getLabel("x","titleLabelBFrame","black");
                JLabel workOYLabel =  NewComp.getLabel("y","titleLabelBFrame","black");
                JLabel workOZLabel =  NewComp.getLabel("z","titleLabelBFrame","black");
                workOCombo = NewComp.getComboBox("textComboBox");
                String[] gnumber = new String[numberWorkOffset];
                for( i = 0; i < numberWorkOffset;i++)
                {
                    listIndexWorkOffsetX[i] = false;
                    listIndexWorkOffsetY[i] = false;
                    listIndexWorkOffsetZ[i] = false;
                    gnumber[i] = "G"+new Integer(54+i);
                    workOCombo.addItem(gnumber[i]);
                }
                workOCombo.setSelectedIndex(activeWOIndex);
                workOffsetX = NewComp.getNumberTxtField("textFieldNumBFrame");
                workOffsetX.setValue(0);
                workOffsetY = NewComp.getNumberTxtField("textFieldNumBFrame");
                workOffsetY.setValue(0);
                workOffsetZ = NewComp.getNumberTxtField("textFieldNumBFrame");
                workOffsetZ.setValue(0);
                if(!offsetAxes_Aux.isEmpty())
                {
                    workOffsetX.setValue(offsetAxes_Aux.get(activeWOIndex)[0]);
                    workOffsetY.setValue(offsetAxes_Aux.get(activeWOIndex)[1]);
                    workOffsetZ.setValue(offsetAxes_Aux.get(activeWOIndex)[2]);
                }
                workOCombo.addItemListener(getListener(
                    gnumber, 
                    workOffsetX, 
                    workOffsetY, 
                    workOffsetZ));
                panel_Aux.add(workOCombo);
                panel_Aux1.add(workOXLabel);
                panel_Aux1.add(workOffsetX);
                panel_Aux2.add(workOYLabel);
                panel_Aux2.add(workOffsetY);
                panel_Aux3.add(workOZLabel);
                panel_Aux3.add(workOffsetZ);
                panel.add(panel_Aux);
                panel.add(panel_Aux1);
                panel.add(panel_Aux2);
                panel.add(panel_Aux3);
                workOffsetX.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            listIndexWorkOffsetX[activeWOIndex] = true;
                            if(changed.lastIndexOf(Id.WORK_OFFSET) == -1)
                                    changed.add(Id.WORK_OFFSET);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            listIndexWorkOffsetX[activeWOIndex] = true;
                            if(changed.lastIndexOf(Id.WORK_OFFSET) == -1)
                                    changed.add(Id.WORK_OFFSET);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                workOffsetY.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            listIndexWorkOffsetY[activeWOIndex] = true;
                            if(changed.lastIndexOf(Id.WORK_OFFSET) == -1)
                                    changed.add(Id.WORK_OFFSET);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            listIndexWorkOffsetY[activeWOIndex] = true;
                            if(changed.lastIndexOf(Id.WORK_OFFSET) == -1)
                                    changed.add(Id.WORK_OFFSET);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                workOffsetZ.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            listIndexWorkOffsetZ[activeWOIndex] = true;
                            if(changed.lastIndexOf(Id.WORK_OFFSET) == -1)
                                    changed.add(Id.WORK_OFFSET);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            listIndexWorkOffsetZ[activeWOIndex] = true;
                            if(changed.lastIndexOf(Id.WORK_OFFSET) == -1)
                                    changed.add(Id.WORK_OFFSET);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                return panel;
            case TOOL_OFFSET:
                //activeTOIndex = Atc.getSelectedToolIndex();
                checkNumberToolOffsets();
                int maxToolIndex = Atc.getMaxToolIndex();
                if(maxToolIndex == 0)
                    activeTOIndex = 0;
                listIndexToolOffsetL = new boolean[maxToolIndex];
                listIndexToolOffsetD = new boolean[maxToolIndex];
                for( i = 0 ; i < maxToolIndex ; i++)
                {
                    listIndexToolOffsetL[i] = false;
                    listIndexToolOffsetD[i] = false;
                }
                toolNmToCurrentSys();
                panel = NewComp.getPanel(
                    "toolOffset",
                    "titleBorderBFrame",
                    new GridLayout(3,1)
                );
                panel_Aux = new JPanel();
                panel_Aux1 = new JPanel();
                panel_Aux2 = new JPanel();
                JLabel toolLenghtLabel =  NewComp.getLabel("lenght","titleLabelBFrame","black");
                JLabel toolRadiusLabel =  NewComp.getLabel("radius","titleLabelBFrame","black");
                toolOCombo = NewComp.getComboBox("textComboBox");
                String[] toolNumber = new String[maxToolIndex+1];
                for( i = 0; i <= maxToolIndex;i++)
                {
                    if(i == 0)
                        toolNumber[i] = "";
                    else if(i <= 9)
                        toolNumber[i] = "0"+ new Integer(i);
                    else
                        toolNumber[i] = ""+new Integer(i)+"";
                    toolOCombo.addItem(toolNumber[i]);
                }
                toolLenght = NewComp.getNumberTxtField("textFieldNumBFrame");
                toolLenght.setValue(0);
                toolRadius = NewComp.getNumberTxtField("textFieldNumBFrame");
                toolRadius.setValue(0);
                if((!offsetTools.isEmpty())&&(activeTOIndex>0))
                {
                    toolLenght.setValue(offsetTools_Aux.get(activeTOIndex-1)[0]);
                    toolRadius.setValue(offsetTools_Aux.get(activeTOIndex-1)[1]);
                }
                toolOCombo.setSelectedIndex(activeTOIndex);
                toolOCombo.addItemListener(getListener(
                    toolNumber, 
                    toolLenght, 
                    toolRadius));
                toolLenght.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            if(activeTOIndex>0)
                                listIndexToolOffsetL[activeTOIndex-1] = true;
                            if(changed.lastIndexOf(Id.TOOL_OFFSET) == -1)
                                    changed.add(Id.TOOL_OFFSET);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            if(activeTOIndex>0)
                                listIndexToolOffsetL[activeTOIndex-1] = true;
                            if(changed.lastIndexOf(Id.TOOL_OFFSET) == -1)
                                    changed.add(Id.TOOL_OFFSET);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                toolRadius.getDocument().addDocumentListener(
                    new DocumentListener() 
                    {
                        public void insertUpdate(DocumentEvent event) 
                        {
                            if(activeTOIndex>0)
                                listIndexToolOffsetD[activeTOIndex-1] = true;
                            if(changed.lastIndexOf(Id.TOOL_OFFSET) == -1)
                                    changed.add(Id.TOOL_OFFSET);
                        }
                        public void removeUpdate(DocumentEvent event) 
                        {
                            if(activeTOIndex>0)
                                listIndexToolOffsetD[activeTOIndex-1] = true;
                            if(changed.lastIndexOf(Id.TOOL_OFFSET) == -1)
                                    changed.add(Id.TOOL_OFFSET);
                        }
                        public void changedUpdate(DocumentEvent event){}
                    }
                );
                /*if(maxToolIndex>0)
                    toolOCombo.setSelectedIndex(activeTOIndex);
                if(maxToolIndex == 0)
                {
                    toolOCombo.setEnabled(false);
                    toolLenght.setEnabled(false);
                    toolRadius.setEnabled(false);
                }
                if(activeTOIndex == 0)
                {
                    toolLenght.setEnabled(false);
                    toolRadius.setEnabled(false);
                }else
                {
                    toolLenght.setEnabled(true);
                    toolRadius.setEnabled(true);
                }*/
                if(activeTOIndex == 0)
                {
                    toolLenght.setEnabled(false);
                    toolRadius.setEnabled(false);
                }
                panel_Aux.add(toolOCombo);
                panel_Aux1.add(toolLenghtLabel);
                panel_Aux1.add(toolLenght);
                panel_Aux2.add(toolRadiusLabel);
                panel_Aux2.add(toolRadius);
                panel.add(panel_Aux);
                panel.add(panel_Aux1);
                panel.add(panel_Aux2);
                return panel;
        }
        return null;
    }
    public String getDataCategoryID()
    {
        return "pathPlanning";
    }
    public String getValue(Id dataID)
    {
        switch(dataID)
        {
            case METRIC_SYSTEM:
                return metricSystemOpt[metricSystem];
            case WORK_OFFSET:
                return "G"+(54+activeWOIndex);
            case MOTION_TYPE:
                return motionTypeOpt[motionType];
            default:
                return null;
        }
    }
    public boolean setCommandValue(int id,int value)
    {
        return initData( id, value);
    }
    public boolean setCommandValue(int id,long value)
    {
        return initData( id, value);
    }
    public boolean setDataValue(int id,int value)
    {
        if(!initData( id, value))
            return false;
        switch(id)
        {
            case MOTION_TYPE:
                motionTypeField.setText(
                    NewComp.getString(motionTypeOpt[motionType]));
                break;
            case CARTESIAN_PLANE:
                cartesianPlaneField.setText(
                    NewComp.getString(cartesianPlaneOpt[cartesianPlane]));
                break;
            case DIMENSIONING_MODE:
                dimModeField.setText(
                    NewComp.getString(dimModeOpt[dimMode]));
            case ATP_X:
                xATPField.setValue(xATP);
                break;
            case ATP_Y:
                yATPField.setValue(yATP);
                break;
            case ATP_Z:
                zATPField.setValue(zATP);
                break;
        }
        return true;
    }
    public boolean setDataValue(int id,long value)
    {
        return setDataValue(id,(int) value);
    }
    public void applyWOChange(int i)
    {
        if(listIndexWorkOffsetX[i] == true)
        {
            listIndexWorkOffsetX[i] = false;
            offsetAxes_Aux.get(i)[0] = ((Number)workOffsetX.getValue()).floatValue();
            if((toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[0].floatValue()) > (long)Integer.MAX_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[0].floatValue()) < (long)Integer.MIN_VALUE))
            {
                offsetAxes_Aux.get(i)[0] = Float.valueOf(
                    toMmOrIn( 
                        metricSystemOpt[metricSystem],
                        offsetAxes.get(i)[0]
                    )
                );
                workOffsetX.setValue(offsetAxes_Aux.get(i)[0]);
                workOCombo.setSelectedIndex(i);
                workOffsetX.requestFocus();
            } else
            {
                offsetAxes.get(i)[0] = (int)toNm(
                    metricSystemOpt[metricSystem], 
                    offsetAxes_Aux.get(i)[0].floatValue());
                InfoManager.addWorkOffsets(i, 0, offsetAxes.get(i)[0]);
            }
        }
        
        if(listIndexWorkOffsetY[i] == true)
        {
            listIndexWorkOffsetY[i] = false;
            offsetAxes_Aux.get(i)[1] = ((Number)workOffsetY.getValue()).floatValue();
            if((toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[1].floatValue()) > (long)Integer.MAX_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[1].floatValue()) < (long)Integer.MIN_VALUE))
            {
                offsetAxes_Aux.get(i)[1] = Float.valueOf(
                    toMmOrIn( 
                        metricSystemOpt[metricSystem],
                        offsetAxes.get(i)[1]
                    )
                );
                workOffsetY.setValue(offsetAxes_Aux.get(i)[1]);
                workOCombo.setSelectedIndex(i);
                workOffsetY.requestFocus();
            } else
            {
                offsetAxes.get(i)[1] = (int)toNm(
                    metricSystemOpt[metricSystem], 
                    offsetAxes_Aux.get(i)[1].floatValue());
                InfoManager.addWorkOffsets(i, 1, offsetAxes.get(i)[1]);
            }
        }
        
        if(listIndexWorkOffsetZ[i] == true)
        {
            listIndexWorkOffsetZ[i] = false;
            offsetAxes_Aux.get(i)[2] = ((Number)workOffsetZ.getValue()).floatValue();
            if((toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[2].floatValue()) > (long)Integer.MAX_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[2].floatValue()) < (long)Integer.MIN_VALUE))
            {
                offsetAxes_Aux.get(i)[2] = Float.valueOf(
                    toMmOrIn( 
                        metricSystemOpt[metricSystem],
                        offsetAxes.get(i)[2]
                    )
                );
                workOffsetZ.setValue(offsetAxes_Aux.get(i)[2]);
                workOCombo.setSelectedIndex(i);
                workOffsetZ.requestFocus();
            } else if(offsetAxes_Aux.get(i)[2] != Float.valueOf(
                    toMmOrIn( 
                        metricSystemOpt[metricSystem],
                        offsetAxes.get(i)[2]
                    )))
            {
                offsetAxes.get(i)[2] = (int)toNm(
                    metricSystemOpt[metricSystem], 
                    offsetAxes_Aux.get(i)[2].floatValue());
                InfoManager.addWorkOffsets(i, 2, offsetAxes.get(i)[2]);
            }
        }
    }
    public void applyTOChange(int i)
    {
        i = i-1;
        if(listIndexToolOffsetL[i] == true)
        {
            listIndexToolOffsetL[i] = false;
            offsetTools_Aux.get(i)[0] = ((Number)toolLenght.getValue()).floatValue();
            if((toNm(metricSystemOpt[metricSystem], offsetTools_Aux.get(i)[0].floatValue()) < (long)Integer.MIN_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetTools_Aux.get(i)[0].floatValue()) > (long)Integer.MAX_VALUE))
            {
                offsetTools_Aux.get(i)[0] = Float.valueOf(
                    toMmOrIn( 
                        metricSystemOpt[metricSystem],
                        offsetTools.get(i)[0]
                    )
                );
                toolLenght.setValue(offsetTools_Aux.get(i)[0]);
                toolOCombo.setSelectedIndex(i);
                toolLenght.requestFocus();
            } else
            {
                offsetTools.get(i)[0] = Integer.valueOf((int)toNm(
                    metricSystemOpt[metricSystem], 
                    offsetTools_Aux.get(i)[0].floatValue())
                );
                InfoManager.addToolOffsets(i+1, 0, offsetTools.get(i)[0]);
            }
        }
        
        if(listIndexToolOffsetD[i] == true)
        {
            listIndexToolOffsetD[i] = false;
            offsetTools_Aux.get(i)[1] = ((Number)toolRadius.getValue()).floatValue();
            if((toNm(metricSystemOpt[metricSystem], offsetTools_Aux.get(i)[1].floatValue()) < (long)Integer.MIN_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetTools_Aux.get(i)[1].floatValue()) > (long)Integer.MAX_VALUE))
            {
                offsetTools_Aux.get(i)[1] = Float.valueOf(
                    toMmOrIn( 
                        metricSystemOpt[metricSystem],
                        offsetTools.get(i)[1]
                    )
                );
                toolRadius.setValue(offsetTools_Aux.get(i)[1]);
                toolOCombo.setSelectedIndex(i);
                toolRadius.requestFocus();
            } else
            {
                offsetTools.get(i)[1] = Integer.valueOf((int)toNm(
                    metricSystemOpt[metricSystem], 
                    offsetTools_Aux.get(i)[1].floatValue())
                );
                InfoManager.addToolOffsets(i+1, 1, offsetTools.get(i)[1]);
            }
        }
    }
    public void applyChange()
    {
        int i;
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case WORK_OFFSET:
                    if(activeWOIndex > 0)
                        applyWOChange(activeWOIndex);
                    /*offsetAxes_Aux.get(activeWOIndex)[0] = ((Number)workOffsetX.getValue()).floatValue();
                    offsetAxes_Aux.get(activeWOIndex)[1] = ((Number)workOffsetY.getValue()).floatValue();
                    offsetAxes_Aux.get(activeWOIndex)[2] = ((Number)workOffsetZ.getValue()).floatValue();
                    i = validateValueWO();
                    if(i != numberWorkOffset)
                    {
                        changeNmToCurrentSys();
                        workOffsetX.setValue(offsetAxes_Aux.get(activeWOIndex)[0]);
                        workOffsetY.setValue(offsetAxes_Aux.get(activeWOIndex)[1]);
                        workOffsetZ.setValue(offsetAxes_Aux.get(activeWOIndex)[2]);
                        return;
                    }
                    changeUnitsToNm();
                    for( i = 0; i < numberWorkOffset;i++)
                        InfoManager.addWorkOffsets(i, offsetAxes.get(i)[0], offsetAxes.get(i)[1], offsetAxes.get(i)[2]);*/
                    break;
                case TOOL_OFFSET:
                    if(activeTOIndex > 0)
                        applyTOChange(activeTOIndex);
                    /*offsetTools_Aux.get(activeTOIndex)[0] = ((Number)toolLenght.getValue()).floatValue();
                    offsetTools_Aux.get(activeTOIndex)[1] = ((Number)toolRadius.getValue()).floatValue();
                    i = validateValueTO();
                    if(i != Atc.getMaxToolIndex())
                    {
                        toolNmToCurrentSys();
                        toolLenght.setValue(offsetTools_Aux.get(activeTOIndex)[0]);
                        toolRadius.setValue(offsetTools_Aux.get(activeTOIndex)[1]);
                        return;
                    }
                    toolAuxUnitsToNm();
                    int maxToolIndex = Atc.getMaxToolIndex();
                    for( i = 0; i < maxToolIndex;i++)
                        InfoManager.addToolOffsets(i, offsetTools.get(i)[0], offsetTools.get(i)[1]);*/
                    break;
                case DEFAULT_METRIC_SYSTEM:
                    InfoManager.addDataValue(METRIC_SYSTEM, defaultMetricSystem);
                    InfoManager.addDataValue(
                        DEFAULT_METRIC_SYSTEM, 
                        defaultMetricSystem
                    );
                    break;
                case DEFAULT_DIMENSIONING_MODE:
                    InfoManager.addDataValue(DIMENSIONING_MODE, defaultDimMode);
                    InfoManager.addDataValue(
                        DEFAULT_DIMENSIONING_MODE, 
                        defaultDimMode
                    );
                    break;
                case OTHER:
                    break;
                default:
                    break;
            }
            changed.remove(0);
        }
    }
    public void cancelChange()
    {
        while( changed.isEmpty() == false)
        {
            switch(changed.get(0))
            {
                case WORK_OFFSET:
                    changeNmToCurrentSys();
                    workOffsetX.setValue(offsetAxes_Aux.get(activeWOIndex)[0]);
                    workOffsetY.setValue(offsetAxes_Aux.get(activeWOIndex)[1]);
                    workOffsetZ.setValue(offsetAxes_Aux.get(activeWOIndex)[2]);
                    break;
                case TOOL_OFFSET:
                    toolNmToCurrentSys();
                    toolLenght.setValue(offsetTools_Aux.get(activeTOIndex-1)[0]);
                    toolRadius.setValue(offsetTools_Aux.get(activeTOIndex-1)[1]);
                    break;
                case DEFAULT_METRIC_SYSTEM:
                    defaultMetricSystem = metricSystem;
                    if(metricSystemOpt[defaultMetricSystem].equals("international"))
                        international.setSelected(true);
                    else
                        english.setSelected(true);
                    break;
                case DEFAULT_DIMENSIONING_MODE:
                    defaultDimMode = dimMode;
                    if(dimModeOpt[defaultDimMode].equals("absolute"))
                        absolute.setSelected(true);
                    else
                        incremental.setSelected(true);
                    break;
                case OTHER:
                    break;
                default:
                    break;
            }
            changed.remove(0);
        }
        if(!changed.isEmpty())
            changed.clear();
    }
    /**
     * Inicializa los datos de las compensación de trabajo.
     * @param x compensación del eje x.
     * @param y compensación del eje y.
     * @param z compensación del eje z.
     */
    public void initWorkOffset(int x,int y,int z)
    {
        Integer[] aux = {
            new Integer(x),
            new Integer(y),
            new Integer(z)
        };
        Float[] auxF = {
            new Float(0),
            new Float(0),
            new Float(0)
        };
        offsetAxes.add(aux);
        offsetAxes_Aux.add(auxF);
    }
    /**
     * Inicializa los datos de compensación de 
     * las herramientas presentes.
     * @param lenght compensación de la longitud de la herramienta.
     * @param diameter compensación del diámetro de la herramienta.
     */
    public void initToolOffset(int lenght, int diameter)
    {
        Integer[] aux = {
            new Integer(lenght),
            new Integer(diameter)
        };
        Float[] auxF = {
            new Float(0),
            new Float(0)
        };
        offsetTools.add(aux);
        offsetTools_Aux.add(auxF);
    }
    /**
     * Proporciona el identificador del sistema métrico en uso.
     * @return una cadena de texto dependiendo si el sistema métrico es 
     * intenacional ("international") o ingles ("english").
     */
    public static String getMetricSystem()
    {
        return metricSystemOpt[metricSystem];
    }
    /**
     * Proporciona el identificador del tipo de movimiento en uso.
     * @return una cadena de texto dependiendo si el tipo de 
     * movimiento es rápido ("rapid"), lieal ("linear"), en dirección
     * de las manecillas del reloj ("clockwise") o en contra de las 
     * manecillas del reloj ("counterClockwise").
     */
    public static String getMotionType()
    {
        return motionTypeOpt[motionType];
    }
    /**
     * Confirma que el número de datos de compensación de tabajo
     * concuerde con el índice máximo de herramienta. Si el índice 
     * es mayor agrega datos de compensación inicializados igual a 
     * cero. Si el índice es menor remueve los datos sobrantes.
     * Si son iguales no hace nada. Estas acciones no afectan a la base
     * de datos a menos que se confirmen.
     */
    private void checkNumberToolOffsets()
    {
        //int maxToolIndex = Atc.getMaxToolIndex();
        int limit = Atc.getMaxToolIndex() - offsetTools.size();
        if(limit == 0)//offsetTools.size() == maxToolIndex)
            return;
        else if(limit > 0)//offsetTools.size() < maxToolIndex)
        {
            for(int i = 0 ; i < limit ; i++)//offsetTools.size()+1 ; i <= maxToolIndex ; i++)
                offsetTools.add(new Integer[] {new Integer(0),new Integer(0),new Integer(0)});
        }
        else if(limit < 0)//offsetTools.size() > maxToolIndex)
        {
            for(int i = 0 ; i < Math.abs(limit) ; i++)//offsetTools.size()-1 ; i > maxToolIndex ; i--)
                offsetTools.remove(0);
        }
    }
    private void changeUnitsToNm()
    {
        if(offsetAxes.isEmpty())
            return;
        for(int i = 0;i <  numberWorkOffset; i++)
        {
            offsetAxes.get(i)[0] = Integer.valueOf((int)toNm(
                metricSystemOpt[metricSystem], 
                offsetAxes_Aux.get(i)[0].floatValue())
            );
            offsetAxes.get(i)[1] = Integer.valueOf((int)toNm(
                metricSystemOpt[metricSystem], 
                offsetAxes_Aux.get(i)[1].floatValue())
            );
            offsetAxes.get(i)[2] = Integer.valueOf((int)toNm(
                metricSystemOpt[metricSystem], 
                offsetAxes_Aux.get(i)[2].floatValue())
            );
        }
    }
    private void changeNmToCurrentSys()
    {
        if(offsetAxes.isEmpty())
            return;
        for(int i = 0;i <  numberWorkOffset; i++)
        {
            offsetAxes_Aux.get(i)[0] = Float.valueOf(
                toMmOrIn( 
                    metricSystemOpt[metricSystem],
                    offsetAxes.get(i)[0]
                )
            );
            offsetAxes_Aux.get(i)[1] = Float.valueOf(
                toMmOrIn( 
                    metricSystemOpt[metricSystem],
                    offsetAxes.get(i)[1]
                )
            );
            offsetAxes_Aux.get(i)[2] = Float.valueOf(
                toMmOrIn(   
                    metricSystemOpt[metricSystem],
                    offsetAxes.get(i)[2]
                )
            );
        }
    }
    private void toolAuxUnitsToNm()
    {
        if(offsetTools.isEmpty())
            return;
        int maxToolIndex = Atc.getMaxToolIndex();
        for(int i = 0; i < maxToolIndex; i++)
        {
            offsetTools.get(i)[0] = Integer.valueOf(
                (int)toNm(
                    metricSystemOpt[metricSystem], 
                    offsetTools_Aux.get(i)[0].floatValue()
                )
            );
            offsetTools.get(i)[1] = Integer.valueOf(
                (int)toNm(
                    metricSystemOpt[metricSystem], 
                    offsetTools_Aux.get(i)[1].floatValue()
                )
            );
        }
    }
    private void toolNmToCurrentSys()
    {
        if(offsetTools.isEmpty())
            return;
        int maxToolIndex = Atc.getMaxToolIndex();
        for(int i = 0; i <  maxToolIndex; i++)
        {
            offsetTools_Aux.get(i)[0] = Float.valueOf(
                toMmOrIn(
                    metricSystemOpt[metricSystem], 
                    offsetTools.get(i)[0]
                )
            );
            offsetTools_Aux.get(i)[1] = Float.valueOf(
                toMmOrIn(
                    metricSystemOpt[metricSystem], 
                    offsetTools.get(i)[1]
                )
            );
        }
    }
    private ItemListener getListener(
        final String[] aux, 
        final JFormattedTextField workOffsetX, 
        final JFormattedTextField workOffsetY, 
        final JFormattedTextField workOffsetZ)
    {
        return new ItemListener() {
            public void itemStateChanged(ItemEvent itemEvent) {
                int i = 0;
                while(aux[i].equals((String)itemEvent.getItem()) != true)
                {
                    i++;
                }
                switch(itemEvent.getStateChange())
                {
                    case ItemEvent.SELECTED:
                        workOffsetX.setValue(offsetAxes_Aux.get(i)[0].floatValue());
                        workOffsetY.setValue(offsetAxes_Aux.get(i)[1].floatValue());
                        workOffsetZ.setValue(offsetAxes_Aux.get(i)[2].floatValue());
                        listIndexWorkOffsetX[activeWOIndex] = false;
                        listIndexWorkOffsetY[activeWOIndex] = false;
                        listIndexWorkOffsetZ[activeWOIndex] = false;
                        InfoManager.addDataValue(ACTIVE_WORK_OFFSET_INDEX, i);
                        break;
                    case ItemEvent.DESELECTED:
                        offsetAxes_Aux.get(i)[0] = ((Number)workOffsetX.getValue()).floatValue();
                        offsetAxes_Aux.get(i)[1] = ((Number)workOffsetY.getValue()).floatValue();
                        offsetAxes_Aux.get(i)[2] = ((Number)workOffsetZ.getValue()).floatValue();
                        break;
                }
            }
        };
    }
    private ItemListener getListener(
        final String[] aux, 
        final JFormattedTextField toolOffsetL, 
        final JFormattedTextField toolOffsetR)
    {
        return new ItemListener() {
            public void itemStateChanged(ItemEvent itemEvent) {
                int i = 0;
                String empty = "";
                if(empty.equals((String)itemEvent.getItem()))
                {
                    toolLenght.setEnabled(false);
                    toolRadius.setEnabled(false);
                    if(itemEvent.getStateChange() == ItemEvent.SELECTED)
                        InfoManager.addDataValue(ACTIVE_TOOL_OFFSET_INDEX, i);
                    return;
                } else
                {
                    toolLenght.setEnabled(true);
                    toolRadius.setEnabled(true);
                    do
                    {
                        i++;
                    }while(aux[i].equals((String)itemEvent.getItem()) != true);
                    switch(itemEvent.getStateChange())
                    {
                        case ItemEvent.SELECTED:
                            toolOffsetL.setValue(offsetTools_Aux.get(i-1)[0].floatValue());
                            toolOffsetR.setValue(offsetTools_Aux.get(i-1)[1].floatValue());
                            listIndexToolOffsetL[i-1] = false;
                            listIndexToolOffsetD[i-1] = false;
                            InfoManager.addDataValue(ACTIVE_TOOL_OFFSET_INDEX, i);
                            break;
                        case ItemEvent.DESELECTED:
                            offsetTools_Aux.get(i-1)[0] = ((Number)toolOffsetL.getValue()).floatValue();
                            offsetTools_Aux.get(i-1)[1] = ((Number)toolOffsetR.getValue()).floatValue();
                            break;
                    }
                }
            }
        };
    }
    /*private int validateValueWO()
    {
        if(offsetAxes.isEmpty())
            return -1;
        for(int i = 0;i <  numberWorkOffset; i++)
        {
            if((toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[0].floatValue()) > (long)Integer.MAX_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[0].floatValue()) < (long)Integer.MIN_VALUE))
            {
                workOCombo.setSelectedIndex(i);
                workOffsetX.requestFocus();
                return i;
            }
            if((toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[1].floatValue()) > (long)Integer.MAX_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[1].floatValue()) < (long)Integer.MIN_VALUE))
            {
                workOCombo.setSelectedIndex(i);
                workOffsetY.requestFocus();
                return i;
            }
            if((toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[2].floatValue()) > (long)Integer.MAX_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetAxes_Aux.get(i)[2].floatValue()) < (long)Integer.MIN_VALUE))
            {
                workOCombo.setSelectedIndex(i);
                workOffsetZ.requestFocus();
                return i;
            }
        }
        return numberWorkOffset;
    }
    private int validateValueTO()
    {
        if(offsetTools.isEmpty())
            return -1;
        int maxToolIndex = Atc.getMaxToolIndex();
        for(int i = 0; i < maxToolIndex; i++)
        {
            if((toNm(metricSystemOpt[metricSystem], offsetTools_Aux.get(i)[0].floatValue()) < (long)Integer.MIN_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetTools_Aux.get(i)[0].floatValue()) > (long)Integer.MAX_VALUE))
            {
                toolOCombo.setSelectedIndex(i);
                toolLenght.requestFocus();
                return i;
            }
            if((toNm(metricSystemOpt[metricSystem], offsetTools_Aux.get(i)[1].floatValue()) < (long)Integer.MIN_VALUE)||
                (toNm(metricSystemOpt[metricSystem], offsetTools_Aux.get(i)[1].floatValue()) > (long)Integer.MAX_VALUE))
            {
                toolOCombo.setSelectedIndex(i);
                toolRadius.requestFocus();
                return i;
            }
        }
        return maxToolIndex;
    }*/
    public static int getActiveTOIndex()
    {
        return activeTOIndex;
    }
    static
    {
        motionType = new Integer(0);
        cartesianPlane = new Integer(0);
        metricSystem = new Integer(0);
        dimMode = new Integer(0);
        xATP = new Integer(0);
        yATP = new Integer(0);
        zATP = new Integer(0);
        activeWOIndex = new Integer(0);
        activeTOIndex = new Integer(0);
        offsetAxes = new ArrayList<Integer[]>(15);
        offsetTools = new ArrayList<Integer[]>(15);
        offsetAxes_Aux = new ArrayList<Float[]>(15);
        offsetTools_Aux = new ArrayList<Float[]>(15);
        defaultMetricSystem = new Integer(0);
        defaultDimMode = new Integer(0);
        numberWorkOffset = new Integer(7);
        motionTypeField = NewComp.getTxtField("default", "textFieldTFrame");
        cartesianPlaneField = NewComp.getTxtField("default", "textFieldTFrame");
        metricSystemField = NewComp.getTxtField("default", "textFieldTFrame");
        dimModeField = NewComp.getTxtField("default", "textFieldTFrame");
        xProgramZeroField = NewComp.getFTF(true,"textFieldNumTFrame");
        yProgramZeroField = NewComp.getFTF(true,"textFieldNumTFrame");
        zProgramZeroField = NewComp.getFTF(true,"textFieldNumTFrame");
    }
    private static final String[] motionTypeOpt = {
        "rapid",
        "linear",
        "clockwise",
        "counterClockwise"
    };
    private static final String[] cartesianPlaneOpt = {
        "xy",
        "xz",
        "yz"
    };
    private static final String[] metricSystemOpt = {
        "international",
        "english"
    };
    private static final String[] dimModeOpt = {
        "absolute",
        "incremental"
    };
    private boolean[] listIndexWorkOffsetX;
    private boolean[] listIndexWorkOffsetY;
    private boolean[] listIndexWorkOffsetZ;
    private boolean[] listIndexToolOffsetL;
    private boolean[] listIndexToolOffsetD;
    private int previousWOIndex;
    private static JComboBox workOCombo;
    private static JComboBox toolOCombo;
    private static Integer motionType;
    private static Integer cartesianPlane;
    private static Integer metricSystem;
    private static JRadioButton international;
    private static JRadioButton english;
    private static Integer dimMode;
    private static JRadioButton absolute;
    private static JRadioButton incremental;
    private static Integer xATP;
    private static Integer yATP;
    private static Integer zATP;
    private static Integer activeWOIndex;
    private static Integer activeTOIndex;
    private static Integer defaultMetricSystem;
    private static Integer defaultDimMode;
    private static Integer numberWorkOffset;
    private static ArrayList <Integer[]> offsetAxes;
    private static ArrayList <Integer[]> offsetTools;
    private static ArrayList <Float[]> offsetAxes_Aux;
    private static ArrayList <Float[]> offsetTools_Aux;
    private static JTextField motionTypeField;
    private static JTextField cartesianPlaneField;
    private static JTextField metricSystemField;
    private static JTextField dimModeField;
    private static JFormattedTextField xATPField;
    private static JFormattedTextField yATPField;
    private static JFormattedTextField zATPField;
    private static JFormattedTextField xProgramZeroField;
    private static JFormattedTextField yProgramZeroField;
    private static JFormattedTextField zProgramZeroField;
    private static JFormattedTextField workOffsetX;
    private static JFormattedTextField workOffsetY;
    private static JFormattedTextField workOffsetZ;
    private static JFormattedTextField toolLenght;
    private static JFormattedTextField toolRadius;
}
